const Router = require('koa-router');
const router = new Router();
const tools = require('../tools/tool')
const {db, QueryTypes, Op} = require("../models/db");
// 删除用户
router.post('/cars/save', async ctx => {
    try {
        let user = ctx.user
        let tenantId = user.tenantId;
        let body = ctx.request.body
        let id = body.id ? body.id : 0;
        let name = body.name ? tools.xe.trim(body.name) : '';
        let licence = body.licence ? tools.xe.trim(body.licence) : '';
        let length = body.length ? body.length : 0;
        let width = body.width ? body.width : 0;
        let height = body.height ? body.height : 0;
        let load = body.load ? body.load : 0;
        let uid = user.id;
        let ctime = tools.getTime()
        if (!name) {
            return ctx.err('请输入名称')
        }
        let save = {
            tenantId,
            licence,
            length,
            width,
            height,
            name,
            load
        }
        if (id) {
            save.uby = uid;
            save.utime = ctime;
            let c = await db.models['cars'].count({
                where: {
                    tenantId,
                    licence,
                    id: {
                        [Op.ne]: id
                    }
                }
            })
            if (c) {
                return ctx.err('已存在相同车牌');
            }
            let old = await db.models['cars'].findOne({
                where: {
                    id
                }
            })
            if (old.isInit == 1 && old.name != name) {
                return ctx.err('不能修改默认设置')
            }
        } else {
            save.cby = uid;
            save.ctime = ctime;
            let c = await db.models['cars'].count({
                where: {
                    licence,
                    tenantId
                }
            })
            if (c) {
                return ctx.err('已存在相同车牌');
            }
        }
        await db.transaction(async t => {
            if(id) {
                await db.models['cars'].update(save, {
                    where: {
                        id
                    }
                })
                save.id = id
            }
            else {
                save.id = await db.models['cars'].create(save)
            }

        })
        return ctx.suc(id ? '修改成功' : '新增成功', save);
    } catch (e) {
        return ctx.err(e)
    }
})
router.post('/cars/del', async ctx => {
    try {
        let user = ctx.user
        let body = ctx.request.body
        let tenantId = user.tenantId;
        let ids = body.ids ? body.ids : [];
        if (ids.length == 0) {
            return ctx.err('未找到数据');
        }
        let data = await db.models['cars'].findAll({
            where: {
                tenantId,
                id: {
                    [Op.in]: ids
                }
            }
        })
        if (data.length == 0) {
            return ctx.err('未找到数据');
        }
        if (data.length != ids.length) {
            return ctx.err('删除条数不等于已知条数');
        }
        await db.models['cars'].update({
            isDel: 1,
            dby: user.id
        }, {
            where: {
                id: {
                    [Op.in]: ids
                }
            }
        })
        return ctx.suc('已删除' + data.length);
    } catch (e) {
        return ctx.err(e)
    }
})


module.exports = router;
